<template>
  <div class="app-container">
    <el-form label-width="120px" :rules="rules" ref="form" :model="form">
      <el-row :gutter="20" type="flex" class="row-bg" justify="start">
        <el-col :xs="18" :sm="12" :md="10" :lg="6" :xl="6">
          <el-form-item label="添加活动类目" prop="dictLabel">
            <el-input
              v-model="form.dictLabel"
              placeholder="请输入活动类目名称"
            ></el-input> </el-form-item
        ></el-col>
        <el-col>
          <el-form-item>
            <el-button type="primary" @click="add">添加</el-button>
          </el-form-item></el-col
        >
      </el-row>
    </el-form>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="createTime" label="添加时间"> </el-table-column>
      <el-table-column prop="dictLabel" label="活动类目名称"> </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:dict:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { addData, listData, delData } from "@/api/system/dict/data";
export default {
  data() {
    return {
      loading: false,
      form: { dictLabel: "" },
      total: 0,
      pageNum: 1,
      pageSize: 10,
      list: [],
      rules: {
        dictLabel: [
          {
            required: true,
            message: "请输入活动类目名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleDelete(row) {
      const dictCodes = row.dictCode || this.ids;
      this.$modal
        .confirm('是否确认删除活动类目名称为"' + row.dictLabel + '"的数据项？')
        .then(function () {
          return delData(dictCodes);
        })
        .then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .catch(() => {});
    },
    getList() {
      this.loading = true;
      listData({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        dictType: "activity_classification",
      })
        .then((res) => {
          this.total = res.total;
          this.list = res.rows;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    add() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addData({
            dictType: "activity_classification",
            dictValue: Date.now(),
            dictLabel: this.form.dictLabel,
          }).then((res) => {
            this.form.dictLabel = "";
            this.$modal.msgSuccess("新增成功");
            this.getList();
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-col-24 {
  width: auto;
}
</style>